package org.jeecg.modules.engineering.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.StringUtils;
import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.modules.engineering.entity.EquipCollectItem;
import org.jeecg.modules.engineering.entity.EquipCollectTrigger;
import org.jeecg.modules.engineering.mapper.EquipCollectItemMapper;
import org.jeecg.modules.engineering.mapper.EquipCollectTriggerMapper;
import org.jeecg.modules.engineering.service.IEquipCollectItemService;
import org.jeecg.modules.engineering.service.IEquipCollectTriggerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * @Description: 设备采集项
 * @Author: jeecg-boot
 * @Date:   2019-09-12
 * @Version: V1.0
 */
@Service
@Slf4j
public class EquipCollectItemServiceImpl extends ServiceImpl<EquipCollectItemMapper, EquipCollectItem> implements IEquipCollectItemService {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private EquipCollectItemMapper equipCollectItemMapper;

    @Autowired
    private EquipCollectTriggerMapper equipCollectTriggerMapper;

    @Autowired
    private IEquipCollectTriggerService equipCollectTriggerService;

    @Override
    @Transactional
    public void saveMain(EquipCollectItem equipCollectItem, List<EquipCollectTrigger> equipCollectTriggerList) {
        int count = equipCollectItemMapper.insert(equipCollectItem);
        for(EquipCollectTrigger entity:equipCollectTriggerList) {
            //外键设置
            entity.setItemId(equipCollectItem.getId());
            entity.setId(UUIDGenerator.getUUID());
            equipCollectTriggerMapper.insert(entity);
        }
        if(count > 0){
            log.info("设置信息到redis中");
            //以ID为key，将小区采集项存储到redis
            setToRedis(equipCollectItem);

        }
    }

    @Override
    @Transactional
    public void updateMain(EquipCollectItem equipCollectItem,List<EquipCollectTrigger> equipCollectTriggerList) {
        EquipCollectItem oldEntity = getById(equipCollectItem.getId());
        int count = equipCollectItemMapper.updateById(equipCollectItem);

        //1.先删除子表数据
        equipCollectTriggerMapper.deleteByMainId(equipCollectItem.getId());

        //2.子表数据重新插入
        for(EquipCollectTrigger entity:equipCollectTriggerList) {
            log.info("外键设置");
            //外键设置
            entity.setItemId(equipCollectItem.getId());
            entity.setId(UUIDGenerator.getUUID());
            equipCollectTriggerMapper.insert(entity);
        }

        if(count > 0){
            //先移除编辑前的redis键值
            removeFromRedis(oldEntity);
            //更新成功后，重新设置redis
            setToRedis(equipCollectItem);
        }
    }

    @Override
    @Transactional
    public void delMain(String id) {
        EquipCollectItem entity = getById(id);
        boolean result = equipCollectTriggerMapper.deleteByMainId(id);
        equipCollectItemMapper.deleteById(id);
        if(result){
            //从redis删除
            removeFromRedis(entity);
        }
    }

    @Override
    @Transactional
    public void delBatchMain(List<String> idList) {
        for(String id:idList) {
            delMain(id);
            //equipCollectTriggerMapper.deleteByMainId(id.toString());
            //equipCollectItemMapper.deleteById(id);
        }
    }

    private void setToRedis(EquipCollectItem entity){
        if(entity == null){
            return;
        }
        redisUtil.set(CacheConstant.REDIS_TABLES+"equip_collect_item:id:"+entity.getId(), JSONObject.toJSONString(entity));
        redisUtil.set(CacheConstant.REDIS_TABLES+"equip_collect_item:otherKey:"+entity.getCollectCode(),entity.getId());

        //存储采集项相关的事件触发规则信息到redis
        equipCollectTriggerService.setToRedis(entity.getId());
    }

    @Override
    public void removeFromRedis(String id){
        removeFromRedis(findById(id));
    }

    @Override
    public void removeFromRedis(EquipCollectItem entity){
        if(entity == null){
            return;
        }
        redisUtil.del(CacheConstant.REDIS_TABLES+"equip_collect_item:id:"+entity.getId(),
                CacheConstant.REDIS_TABLES+"equip_collect_item:otherKey:"+entity.getCollectCode());

        //从redis中删除采集项相关的事件触发规则信息
        equipCollectTriggerService.removeFromRedis(entity.getId());
    }


    @Override
    public EquipCollectItem getFromRedisByOtherKey(String key){
        String id = (String) redisUtil.get(CacheConstant.REDIS_TABLES+"equip_collect_item:otherKey:"+key);
        if(StringUtils.isEmpty(id)){
            return null;
        }
        return getFromRedisById(id);
    }

    @Override
    public EquipCollectItem getFromRedisById(String id){
        EquipCollectItem entity = JSONObject.parseObject((String) redisUtil.get(CacheConstant.REDIS_TABLES+"equip_collect_item:id:"+id),EquipCollectItem.class) ;
        if(entity == null){
            //从数据库查询
            entity = findById(id);
            if(entity != null){
                //重新设置redis
                setToRedis(entity);
            }
        }
        return entity;
    }

    private EquipCollectItem findById(String id) {
        return equipCollectItemMapper.findById(id);
    }

    @Override
    public Result<String> refreshToRedis() {
        List<EquipCollectItem> lists = list();
        if(lists != null && lists.size() > 0){
            for (EquipCollectItem entity : lists){
                setToRedis(entity);
            }
        }
        return Result.ok();
    }

    @Override
    public IPage<EquipCollectItem> selectSqlList(Page<EquipCollectItem> page, String collectName, String collectCode) {
        return page.setRecords(equipCollectItemMapper.selectSqlList(page,collectName,collectCode));
    }

}
